package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.clientCenter.bean.ValveCloseCountBean;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseMapper;
import com.koron.css2.clientCenter.queryBean.ValveCloseQueryBean;
import com.koron.css2.clientCenter.vo.ValveCloseListVo;
import com.koron.css2.clientCenter.vo.ValveCloseVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 表阀门关闭详情 详情查询
 *
 * @author cd
 * @author modify by 杨嘉慧
 */
public class ValveCloseQuery implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<HashMap> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", HashMap.class);

        try {
            ValveCloseQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), ValveCloseQueryBean.class);
            ValveCloseMapper valveCloseMapper = factory.getMapper(ValveCloseMapper.class);
            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);
            ValveCloseVo vo = valveCloseMapper.getValveCloseVo(queryBean);
            List<String> ids = new ArrayList<>();
            ids.add(queryBean.getValveCloseId());
            List<ValveCloseCountBean> countList = detailMapper.countByValveIds(ids);
            BeanUtils.copyProperties(countList.get(0), vo);
            vo.setFinishNum(vo.getCloseNum() + vo.getCancelNum());
            vo.setWaitOpenNum(vo.getOpenSum() - vo.getOpenNum());
            if (!StringUtils.isEmpty(vo.getBusinessArea())) {
                BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
                vo.setBusinessAreaName(
                        businessAreaMapper.findBusinessAreaById(vo.getBusinessArea(), userInfo.getCurWaterCode()));
            }
            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            List<ValveCloseListVo> voList = detailMapper.closeList(queryBean);
            //获取阀门状态字典
            Map<String, String> dics = Tools.mapDicByCode(factory, "VMMVS");
            //根据欠费欠费数判断欠费状态
            for (ValveCloseListVo listVo : voList) {
                if (listVo.getArrearage() != null && 1 == listVo.getArrearage().compareTo(BigDecimal.ZERO)) {
                    listVo.setFeeStatus("是");
                } else {
                    listVo.setFeeStatus("否");
                }
                //是否阀控表
                if (null != listVo.getValveControlFlag() && "1".equals(listVo.getValveControlFlag())) {
                    listVo.setValveControlFlag("是");
                } else {
                    listVo.setValveControlFlag("否");
                }
                //是否远程表
                if (null != listVo.getRemoteFlag() && "1".equals(listVo.getRemoteFlag())) {
                    listVo.setRemoteFlag("是");
                } else {
                    listVo.setRemoteFlag("否");
                }
                //是否取消关阀
                if (null != listVo.getCancelFlag() && "1".equals(listVo.getCancelFlag())) {
                    listVo.setCancelFlag("是");
                } else {
                    listVo.setCancelFlag("否");
                }
                //通过字典表替换阀门状态为中文
                if (null != listVo.getValveStatus() && null != dics.get(listVo.getValveStatus())) {
                    String valveStatusName = dics.get(listVo.getValveStatus());
                    listVo.setValveStatusName(valveStatusName);
                }

            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("ValveCloseVo", vo);
            map.put("list", new PageInfo<>(voList));
            info.setData(map);
            return info;

        } catch (Exception e) {
            logger.error("表阀门关闭详情 查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }

}
